/*   
 * Copyright (c) 2010-2020 DMGIS Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * DMGIS. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with DMGIS.   
 *   
 */
package com.dmgis.qxfw.dataservice.businessservice.dmgis_mongodb_srv.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

/**
 * json序列化工具类
 */
public class JsonUtil {
	private static final ObjectMapper mapper = new ObjectMapper();
	static {
		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	}

	public static String object2Json(Object obj) throws JsonProcessingException {
		return mapper.writeValueAsString(obj);
	}

	public static <T> T jsonStr2Object(String jsonStr, Class<T> clazz) throws IOException {
		return mapper.readValue(jsonStr, clazz);
	}

	public static <T> T jsonNode2Object(JsonNode jsonNode, Class<T> clazz) throws JsonProcessingException {
		return mapper.treeToValue(jsonNode, clazz);
	}

	public static JsonNode jsonStr2JsonNode(String jsonStr) throws IOException {
		return mapper.readTree(jsonStr);
	}

	public static JsonNode Object2JsonNode(Object obj) {
		return mapper.valueToTree(obj);
	}

	public static JsonNode inputStreamJsonNode(InputStream inputStream) throws JsonProcessingException, IOException {
		return mapper.readTree(inputStream);
	}

	/**  
	 * 序列化到List集合中，默认日期样式：yyyy-MM-dd HH:mm:ss
	 * @param jsonStr
	 * @param valueTypeRef
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException  
	 * @author dsq
	 * @date 2018年8月9日
	 */   
	public static <T> T jsonStr2TypeReference(String jsonStr, TypeReference<T> valueTypeRef)
			throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(jsonStr, valueTypeRef);
	}
	/**  
	 * 序列化到list集合中，可指定日期样式
	 * @param jsonStr
	 * @param valueTypeRef
	 * @param dateFormat 日期格式
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException  
	 * @author dsq
	 * @date 2018年8月9日
	 */   
	public static <T> T jsonStr2TypeReference(String jsonStr, TypeReference<T> valueTypeRef, String dateFormat)
			throws JsonParseException, JsonMappingException, IOException {
		mapper.setDateFormat(new SimpleDateFormat(dateFormat));
		return mapper.readValue(jsonStr, valueTypeRef);
	}
}
